import store from "@/store"

function getLocalData(cfg) {
	let data = uni.getStorageSync("eye-revive");
	if (!data) {
		data = [{
			sports: [{
					"date": 1712491968208, // 7
					"totalTime": 60 * 25, // 今天运动的总时间 (分钟)
					// 眼睛的健康习惯
					"eyeHealthHabits": [1, 1, 1, 1, 0]
				},
				{
					"date": 1712591968208, // 1
					"totalTime": 60 * 20,
					"eyeHealthHabits": [0, 1, 0, 1, 1]
				},
				{
					"date": 1712671968208, // 2
					"totalTime": 60 * 10,
					"eyeHealthHabits": [1, 0, 0, 1, 0]
				},
				{
					"date": 1712721968208, // 3
					"totalTime": 60 * 20,
					"eyeHealthHabits": [1, 1, 1, 1, 1]
				},
				{
					"date": 1712780551493, // 4
					"totalTime": 60 * 30,
					"eyeHealthHabits": [1, 1, 0, 0, 1]
				},
				{
					"date": 1712851968208, // 5
					"totalTime": 60 * 30,
					"eyeHealthHabits": [1, 0, 1, 1, 1]
				},
				{
					"date": 1712991968208, // 6
					"totalTime": 60 * 26,
					"eyeHealthHabits": [1, 0, 0, 1, 1]
				}
			],
			settings: {
				reminder: {
					reminderTime: ["09:00", "13:00", "21:00"],
					repeatDays: [1, 1, 1, 1, 1, 0, 0],
					reminderMode: "alert",
					voiceVolume: 50,
					soundEffectVolume: 50
				},
				eyeTrackingSensitivity: 50
			},
			username: "lingling",
			password: "123456"
		}]
		uni.setStorageSync("eye-revive", JSON.stringify(data));
		return data;
	}
	return JSON.parse(data);
}

// 获取基础的用户数据
function getBaseUserData() {
	return {
		data: {
			sports: [],
			settings: {
				reminder: {
					// 每天的提醒时间, 早上 下午 睡前 "00:00"表示不提醒, 默认 "00:00"
					reminderTime: ["09:00", "13:00", "21:00"],
					// 提醒的重复天数(周一到周日), 1表示提醒, 0表示不提醒, 默认值 [1,1,1,1,1,1,1]
					repeatDays: [1, 1, 1, 1, 1, 0, 0],
					// 提醒方式, 值为 none/alert/vibrate/alertAndVibrate (弹窗和震动的组合, none表示不提醒), 默认 "alert"
					reminderMode: "alert",
					voiceVolume: 50, // 语音大小, 默认值50,范围0-100
					soundEffectVolume: 50, // 音效大小, 默认值50,范围0-100
				},
				eyeTrackingSensitivity: 50, // 眼球追踪灵敏度, 默认值50,范围0-100})
			}
		},
	}
}

// 获取基础的当天运动数据
function getBaseTodaySportsData() {
	return {
		date: new Date().getTime(), // 时间戳(Unix时间戳,毫秒数)
		totalTime: 0, // 今天运动的总时间 (分钟)
		eyeHealthHabits: [0, 0, 0, 0, 0], // 眼睛的健康习惯
	}
}

function getUserData(cfg, data) {
	const username = store.state.user.name;
	const user = data.find((item) => item.username === username);
	return user;
}

// 判断是否是同一天
function isSameDay(timestamp1, timestamp2) {
	const date1 = new Date(timestamp1);
	const date2 = new Date(timestamp2);
	return date1.getFullYear() === date2.getFullYear() &&
		date1.getMonth() === date2.getMonth() &&
		date1.getDate() === date2.getDate();
}

function register(cfg, data) {
	const username = cfg.data.username;
	const password = cfg.data.password;
	let user = data.find((item) => item.username === username);
	if (!user) {
		user = getBaseUserData();
		user.username = username;
		user.password = password
		data.push(user);
		uni.setStorageSync("eye-revive", JSON.stringify(data));
		return {
			code: 0,
			msg: "注册成功"
		}
	}
	return {
		code: 406,
		msg: "用户已存在"
	}
}

function login(cfg, data) {
	const username = cfg.data.username;
	const password = cfg.data.password;
	const user = data.find((item) => item.username === username);
	if (!user) {
		return {
			code: 406,
			msg: "用户不存在"
		}
	}
	if (user.password === password) {
		return {
			code: 0,
			msg: "登录成功"
		}
	} else {
		return {
			code: 406,
			msg: "密码错误"
		}
	}
}

// 获取当天的运动数据
function getTodayProgress(cfg, userData) {
	const date = new Date().getTime();
	let sportsData = userData.sports.find((item) => isSameDay(date, item.date));
	if (!sportsData) {
		sportsData = getBaseTodaySportsData();
	}
	return {
		code: 0,
		data: sportsData,
	}
}

// 设置当天的运动数据
function setTodayProgress(cfg, data, userData) {
	const username = store.state.user.name;
	const date = new Date().getTime();
	const sportsData = userData.sports;
	let todayData;
	sportsData.forEach((item, i) => {
		if (isSameDay(date, item.date)) {
			todayData = item;
			sportsData[i] = cfg.data;
		}
	});
	if (!todayData) {
		sportsData.push(cfg.data);
	}
	data.forEach((item, i) => {
		if (item.username === username) {
			data[i].sports = sportsData;
		}
	});
	uni.setStorageSync("eye-revive", JSON.stringify(data));
	return {
		code: 0,
		msg: "设置成功",
	}
}

// 获取本周的运动数据
function getWeekProgress(cfg, userData) {
	const curDate = new Date();
	const dayOfWeek = curDate.getDay();
	const t = curDate.getTime();
	const dayArr = [];
	for (let i = 0; i <= dayOfWeek; i++) {
		const dis = 1000 * 60 * 60 * 24;
		const curT = t - i * dis;
		dayArr.push(curT);
	}
	const arr = userData.sports.filter((item) => {
		const date = new Date(item.date);
		if (dayArr.find((i) => isSameDay(i, date))) {
			return item;
		}
	});
	return {
		code: 0,
		data: arr,
	}
}

// 获取设置的数据
function getSettings(cfg, userData) {
	return {
		code: 0,
		data: userData.settings,
	}
}

// 设置设置的数据
function setSettings(cfg, data) {
	const username = store.state.user.name;
	data.forEach((item, i) => {
		if (item.username === username) {
			data[i].settings = cfg.data;
		}
	});
	uni.setStorageSync("eye-revive", JSON.stringify(data));
	return {
		code: 0,
		data: "修改成功",
	}
}

export function setInfo(cfg) {
	const str = cfg.method + cfg.url;
	const data = getLocalData(cfg);
	let res;
	if (str == "post/register") {
		res = register(cfg, data);
		return res;
	} else if (str == "post/login") {
		res = login(cfg, data);
		return res;
	} else if (str == "get/getInfo") {
		return {
			code: 0,
			data: cfg.data
		}
	}
	const userData = getUserData(cfg, data);
	if (!userData) {
		return {
			code: 406,
			msg: "用户不存在"
		}
	}
	if (str == "get/sports-data/today") {
		res = getTodayProgress(cfg, userData);
	} else if (str == "put/sports-data/today") {
		res = setTodayProgress(cfg, data, userData);
	} else if (str == "get/sports-data/weekly") {
		res = getWeekProgress(cfg, userData);
	} else if (str == "get/settings") {
		res = getSettings(cfg, userData);
	} else if (str == "post/settings") {
		res = setSettings(cfg, data);
	}
	return res;
}